<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">邮件状态</label>
                            <div class="layui-input-inline">
                                <select name="status" lay-verify="" >
                                    <option value="">请选择</option>
                                    <option value="0">未处理</option>
                                    <option value="1">已处理</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            {{#  if(d.status== "0"){ }}
            <button class="layui-btn layui-btn-normal layui-btn-xs data-add-btn" lay-event="picture"> 图片 </button>
            <button class="layui-btn layui-btn-xs  layui-btn-danger data-delete-btn" lay-event="mark"> 标记 </button>
            {{#  } }}
            {{#  if(d.status== "1"){ }}
            <button class="layui-btn layui-btn-normal layui-btn-xs data-add-btn" lay-event="picture"> 图片 </button>
            <button class="layui-btn layui-btn-xs  data-delete-btn" lay-event="delete"> 删除 </button>
            {{#  } }}
        </script>
    </div>
</div>

<script>
    layui.use(['form', 'table','miniPage','element', 'axios'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            axios = layui.axios,
            miniPage = layui.miniPage;

        form.render();
        //渲染表格
        table.render({
            elem: '#currentTableId',
            method: 'post',
            url: 'email/adminEmail',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field: 'title', title: '标题'},
                {field: 'content', title: '内容'},
                {field: 'memberId', title: '发送者', width: 150, templet: function (data) {
                    return data.familyMember.name;
                }},
                {field: 'email', width: 200, title: '发送者邮箱', templet: function (data) {
                    return data.familyMember.email;
                }},
                {field: 'status', title: '状态', templet: function (data) {
                    if (data.status == 0)
                        return `<span class="layui-badge layui-bg-red">未处理</span>`;
                    return `<span class="layui-badge layui-bg-cyan">已处理</span>`;
                }},
                {field: 'createDate', title: '时间'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        /**
         * 工具监听
         */
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            window.email = data.id;
            // console.log(data);
            if (obj.event === 'picture') {
                console.log('http://localhost:8080/email/feedbackImg?img=' + data.img);
                var html = '<div style="padding:15px 20px; text-align:justify; line-height: 22px;border-bottom:1px solid #e2e2e2;background-color: #2f4056;color: #ffffff">\n' +
                    '<div style="font-size: 12px">' + '<img src="' + "http://localhost:8080/email/feedbackImg?img=" + data.img +'" alt="" width="100%" height="200px"/>' + '</div>\n' +
                    '</div>\n';
                parent.layer.open({
                    type: 1,
                    title: "反馈截图" + '<span style="float: right;right: 1px;font-size: 12px;color: #b1b3b9;margin-top: 1px">' + data.createDate + '</span>',
                    area: '300px;',
                    shade: 0.8,
                    id: 'layuimini-notice',
                    btn: ['确定'],
                    btnAlign: 'c',
                    moveType: 1,
                    content: html
                });

                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除数据么', function (index) {
                    //异步请求删除
                    axios({
                        method: 'get',
                        url: 'email/deleteEmail?ids=' + obj.data.id //将数组中的项按,分隔拼接成字符串
                    }).then(response => {
                        layer.msg(response.msg, function (){
                            layui.table.reload("currentTableId");
                            layer.close(index);
                        })
                    }).catch(error => {
                        layer.msg(error);
                    });
                });
            } else if (obj.event === "mark"){
                axios({
                    method: "get",
                    url: "email/markEmail?id="+ obj.data.id
                }).then(response => {
                    layer.msg(response.msg, function () {
                        layui.table.reload("currentTableId");
                    });
                }).catch(error => {
                    console.log(error);
                })

            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: data.field
            }, 'data');

            return false;
        });
        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

    });
</script>